<?php
/**
 * Framework_Request_Peopletab
 *
 * @author      Ian Szewczyk <roamzero@gmail.com>
 * @copyright   (c) 2007 Ian Szewczyk. All rights reserved.
 * @license     http://www.opensource.org/licenses/bsd-license.php
 * @package     Framework
 * @filesource
 */

/**
 * Framework_Request_Peopletab
 *
 * Parses the $_GET request and then sets the module, class and event that
 * needs to be set.
 *
 * @author      Ian Szewczyk <roamzero@gmail.com>
 * @package     Framework
 * @see         Framework_Request
 */
class Framework_Request_Peopletab extends Framework_Request_Common
{
	/**
	 * $lang
	 *
	 * @access      public
	 * @var         string      $lang
	 */
	public $lang = 'en';

	/**
	 * $country
	 *
	 * @access      public
	 * @var         string      $country
	 */
	public $country = 'US';

	/**
	 * $locale
	 *
	 * @access      public
	 * @var         string      $locale
	 */
	public $locale = 'US';

	/**
	 * $requestParts
	 *
	 * @access      public
	 * @var         array      $locale
	 */
	public $requestParts;

	/**
	 * $ownerName
	 *
	 * @access      public
	 * @var         string      $ownerName
	 */
	public $ownerName = '-';

	/**
	 * $owner
	 *
	 * @access      public
	 * @var         Framework_Owner $owner
	 */
	public $owner;

	/**
	 * __construct
	 *
	 * Parses the $_GET request and throws Exceptions if there are any
	 * problems with the request.
	 *
	 * @access      public
	 * @return      void
	 * @throws      Framework_Exception
	 */
	public function __construct()
	{
			
		if (strlen($_SERVER['PATH_INFO']) || strlen($_SERVER['ORIG_PATH_INFO'])) {
			$requestString = strlen($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO']: $_SERVER['ORIG_PATH_INFO'];
			$requestString = trim($requestString,'/');
		} else {
			$requestString = '';
		}
		if (!strlen($requestString)) {
			$requestString = Framework::$site->config('Framework', 'defaultActionPath');
			$requestString = trim($requestString,'/');
		}
	
		$requestParts = explode('/',$requestString);

		if(count($requestParts)) {
			foreach ($requestParts as $val){
				if (!preg_match('/^[_\-\.A-Z0-9]+$/i',$val)) {
					throw new Framework_Exception('Invalid Request Arguments');
				}
			}
		}

		// Owner NEEDS fixing
		$this->ownerName = $requestParts[0];
		$owner = Framework_Owner::singleton(null, $this->ownerName);
			
		array_shift($requestParts);

		// Module
		if (!strlen($requestParts[0]) && $owner->isValid) {
			$this->module = $owner->defaultModule;
		} else {
			$this->module = ucfirst($requestParts[0]);
			array_shift($requestParts);
		}

		// Class
		if(strlen($requestParts[0]) && Framework::$util->file_existsIncPath('Framework/Module/'.$this->module.'/'.ucfirst($requestParts[0]).'.php')) {
			$this->class = $requestParts[0];
			array_shift($requestParts);
		}
			
			
			
		$this->requestParts = $requestParts;


		//$neg = new I18Nv2_Negotiator();
		//$this->lang = $neg->getLanguageMatch();
		//$this->country = $neg->getCountryMatch($this->lang);
		//$this->locale = $neg->getLocaleMatch();


	}

	/**
	 * isValidOwner
	 *
	 * @access 		public
	 * @return 		boolean		whether the owner is valid (exists in the DB) or not
	 */
	public function isValidOwner()
	{
		if ($this->ownerName == '-') {
			return false;
		} else {
				
		}
	}

	/**
	 * getEvent
	 *
	 * @access 		public
	 * @param		object		$module		Instance of Framework_Module
	 * @return 		mixed 		string event, PEAR_Error on failure
	 */
	public function getEvent(Framework_Module $module)
	{
		if (method_exists($module, $this->requestParts[0]) && in_array($this->requestParts[0], $module->validEvents)) {
			$this->event = array_shift($this->requestParts);
			return $this->event;
		} elseif (method_exists($module, '__default')) {
			return '__default';
		} elseif (!method_exists($module, '__call')) {
			return PEAR::raiseError('Module Event Error, could not handle event');
		}

	}
}

?>